package com.liuyi.netty.im.lychat.modular.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuyi.netty.im.lychat.modular.caches.MessageRecordsCache;
import com.liuyi.netty.im.lychat.modular.entity.MessageRecords;
import com.liuyi.netty.im.lychat.modular.enums.MessageType;
import com.liuyi.netty.im.lychat.modular.enums.SessionType;
import com.liuyi.netty.im.lychat.modular.mapper.MessageRecordsMapper;
import com.liuyi.netty.im.lychat.modular.service.IMessageRecordsService;
import com.liuyi.netty.im.server.util.ImUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 消息记录表 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-12-29
 */
@Service
public class MessageRecordsServiceImpl extends ServiceImpl<MessageRecordsMapper, MessageRecords> implements IMessageRecordsService {

    @Resource
    private MessageRecordsCache cache;

    @Override
    public MessageRecordsCache cache() {
        return cache;
    }

    @Override
    public MessageRecords createMessage(String fromId, String toId, SessionType sessionType, MessageType contentType, Object content) {
        MessageRecords records = new MessageRecords();
        records.setFromId(fromId).setToId(toId).setSessionType(sessionType)
                .setContentType(contentType).setContent(ImUtil.encodePlayload(content));
        this.save(records);
        return records;
    }
}
